#!/bin/bash

BinHome="/opt/server.lan"
MntHome="/mnt/server.lan"
TmpHome="/tmp/server.lan"

shellForPort48080(){
Xname="file_server_http"
Xport="48080"
Xbin="/root/$Xname$Xport"
Xdir="${MntHome}/data/dataStorage/server.lan/share/"


if [ ! -e "$Xbin" ];then
    echo "date; echo [INFO] args=\$*" > "$Xbin"
    chmod u+x "$Xbin"
fi

cat > "${BinHome}/custom_entry.sh" <<EOF
#!/bin/bash


Xbin="$Xbin"
Xdir="$Xdir"
Xport="$Xport"

if [ ! -e "\$Xbin" ];then
        echo "[Err] can't find binary file : \$Xbin , exit 1"
        exit 1
fi


flag=false
for i in \`seq 1 20\`;do
        if mountpoint "${MntHome}/data/";then
                flag=true
                echo "[INFO] flag==true, goto Start"
                break
        fi
        echo "try \$i"
        sleep \$i
done

startServer(){
        echo "[INFO] Start server @port :\$Xport"
        "\$Xbin" "\$Xport" "\$Xdir"   >  /tmp/$Xname$Xport.log
}

if \$flag && [ -e "\$Xdir" ];then
        startServer &
fi
EOF
}

# no need , because run docker container. start Manually with https:// ... cmd.
#shellForPort48080

